<script setup lang="ts">
import { reactive } from 'vue'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Table } from '@/components/Table'
import { adminShopPayLogListApi } from '@/api/shop/payLog'

import { useTable } from '@/hooks/web/useTableTest'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'

// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: adminShopPayLogListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'pay_log_id',
    label: '主键',
    width: 60
  },
  {
    field: 'type',
    label: '类型',
    width: 80,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel('shop_pay_log_type', cellValue) : ''
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_pay_log_type
      },
      show: true
    }
  },
  {
    field: 'owner_type',
    label: '所属类型',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel('shop_pay_log_owner_type', cellValue) : ' '
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_pay_log_owner_type
      },
      show: true
    }
  },
  {
    field: 'owner_name',
    label: '所属名称',
    width: 120,
    search: {
      show: true
    }
  },
  // {
  //   field: 'owner_shop_name',
  //   label: '所属店铺名称',
  //   width: 120
  // },
  // {
  //   field: 'owner_user_name',
  //   label: '所属用户名称',
  //   width: 120
  // },
  {
    field: 'amount',
    label: '金额'
  },
  {
    field: 'rel_type',
    label: '关联类型',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return cellValue != '0' ? getDictLabel('shop_pay_log_rel_type', cellValue) : ' '
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.shop_pay_log_rel_type
      },
      show: true
    }
  },
  {
    field: 'rel_sn',
    label: '关联编号',
    width: 180,
    search: {
      show: true
    }
  },
  // {
  //   field: 'rel_order_sn',
  //   label: '关联订单编号',
  //   width: 180
  // },
  // {
  //   field: 'rel_withdrawal_sn',
  //   label: '关联提现单编号',
  //   width: 180
  // },
  {
    field: 'create_time',
    label: '创建时间',
    width: 180
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

/****************************** 初始化 start ******************************/

const init = async () => {
  // 先调店铺列表接口
  await getList()
}
init()

/****************************** 初始化 end ******************************/
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
    />
  </ContentWrap>
</template>
